package com.ken.springcloud.controller;

import com.ken.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <ul>
 * <li>Title: DeptController</li>
 * <li>Description: TODO </li>
 * </ul>
 *
 * @author swc
 * @date 2021-08-26 11:28
 */
@RestController
public class DeptController {


    @Autowired
    private RestTemplate restTemplate;

    //private final  String REQUEST_URL_PREFIX ="http://localhost:8001";

    // 通过ribbon 服务 访问 只需要把地址改为应用名称 即可 （详情见Eureka启动后注册的服务）
    private final  String REQUEST_URL_PREFIX ="http://SPRINGCLOUD-PROVIDER-DEPT";


    @GetMapping("/consumer/add")
    public boolean addDept(Dept dept) {
        return restTemplate.postForObject(REQUEST_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }

    @GetMapping("/consumer/get/{id}")
    public Dept queryById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(REQUEST_URL_PREFIX+"/dept/get/"+id,Dept.class);
    }

    @GetMapping("/consumer/list")
    public List<Dept> queryAll() {
        return restTemplate.getForObject(REQUEST_URL_PREFIX+"/dept/list",List.class);
    }
}
